from pathlib import Path
import cv2, sys, os
from common.config import IMAGE_PATH
# file_path=sys.argv[1] if sys.argv[1:] else "F:\\05_autotest_proj\\auto_get_video_pixel\\data"
# frame_num=sys.argv[2] if sys.argv[2:] else 180

class GetVideoScreenshot(object):
    def __init__(self, file_path, frame_num=100):
        self.file_path=file_path
        self.frame_num=frame_num
        os.chdir(IMAGE_PATH)
        files=list(os.listdir(IMAGE_PATH))
        for file in files:
            if os.path.isfile(file):
                os.remove(file)

    def get_video_screenshot(self):
        for item in Path(self.file_path).rglob('*.mp4'):
            filename=Path(item).stem
            new_path=Path(item).parent
            # path_instan=Path(item)
            # print(path_instan.stem)
            # print(path_instan.parent)
            # print(path_instan.name)
            # print(path_instan.suffix)
            cap=cv2.VideoCapture(str(item))
            cap.set(cv2.CAP_PROP_POS_FRAMES, float(self.frame_num))
            if cap.isOpened():
                rval, frame=cap.read()
                # newFile=os.path.join(str(new_path), str(filename)+".jpg")
                newFile = os.path.join(IMAGE_PATH, f"{str(filename)}.jpg")
                cv2.imencode('.jpg', frame)[1].tofile(newFile)
                print(newFile)
            cap.release()

if __name__=="__main__":
    instan=GetVideoScreenshot("F:\\05_autotest_proj\\auto_get_video_pixel\\data")
    instan.get_video_screenshot()